package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.entity.SpuImage;
import com.atguigu.gmall.model.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.model.product.vo.SaveSpuInfoDetailVo;
import com.atguigu.gmall.model.product.vo.SpuSaleInfoVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:lyd
 **/
@RestController
@RequestMapping("/admin/product")
public class SpuInfoController {

    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    SpuImageService spuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    /**
     * 分页查询SpuInfo信息
     *
     * @param current
     * @param size
     * @param category3Id
     * @return
     */
    @GetMapping("/{current}/{size}")
    public Result list(@PathVariable("current") Long current,
                       @PathVariable("size") Long size,
                       @RequestParam("category3Id") Long category3Id) {
        Page<SpuInfo> page = spuInfoService.lambdaQuery()
                .eq(SpuInfo::getCategory3Id, category3Id)
                .page(new Page<>(current, size));
        return Result.ok(page);
    }

    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SaveSpuInfoDetailVo vo) {
        spuInfoService.saveSpuInfo(vo);
        return Result.ok();
    }

    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable("spuId") Long spuId) {
        return Result.ok(spuImageService.lambdaQuery()
                .eq(SpuImage::getSpuId, spuId)
                .list());
    }

    /**
     * 根据spuId查询spu销售属性列表
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId) {
        List<SpuSaleInfoVo> spuSaleInfoVos = spuSaleAttrService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleInfoVos);
    }
}
